#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

char *readFile(char *f_path);
void copy(char *s, char *d);

void main()
{
  srand(time(NULL));

  char *file = "D:\\docker\\docker-compose\\eureka\\docker-compose.yml";
  // FILE *fp = fopen(file, "rb"); // open file
  // fseek(fp, 0, SEEK_END);
  // int file_size = ftell(fp);
  // rewind(fp);
  // printf("size=%d\n", file_size);
  // char *str;
  // str = (char*)malloc(file_size);
  // fread(str, sizeof(char), file_size - 1, fp);
  // printf("%s\n", str);
  // free(str);
  // fclose(fp);

  // char* content = readFile(file);
  // printf("%s", content);
  // free(content);

  char *dest = "C:\\Users\\byref\\Desktop\\compose.yml";
  copy(file, dest);
}

void copy(char *s, char *d)
{
  FILE *s_stream = fopen(s, "r");
  FILE *d_stream = fopen(d, "w");
  char c;
  while (!feof(s_stream))
  {
    // putchar(fgetc(s_stream));
    c = fgetc(s_stream);
    if (c != 0xFF)
    {
      fputc(c, d_stream);
    }
  }
  fclose(s_stream);
  fclose(d_stream);
  printf("copy success");
}

char *readFile(char *f_path)
{
  FILE *fp = fopen(f_path, "rb");
  if (fp == NULL)
  {
    printf("file not found");
    return NULL;
  }
  fseek(fp, 0, SEEK_END);
  long f_size = ftell(fp);
  rewind(fp);
  char *c = (char *)malloc(f_size);
  fread(c, sizeof(char), f_size - 1, fp);
  // free(c);
  fclose(fp);
  return c;
}